package me.shurufa.db;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;

import me.shurufa.MainApplication;
import me.shurufa.bean.UserInfo;

/**
 * Create by andy (https://github.com/andyxialm)
 * Create time: 16/5/11 13:25
 * Description : 持久化存储工具类，主要用于处理SharedPerferences
 */
public class PersistenceUtils {

    /**
     * 用户
     */
    private static final String USER_PREF_NAME = "user_pref";
    private static final String USER_ID = "user_id";
    private static final String SRF_KEY = "srf_key";
    private static final String SRF_AUTH = "srf_auth";
    private static final String USERNAME = "username";
    private static final String AVATAR = "avatar";
    private static final String MOBILE = "mobile";
    private static final String CREATED_AT = "created_at";
    private static final String DESCRIPTION = "description";
    private static final String NICKNAME = "nickname";
    private static final String CREDITS  = "credits_available";

    private static final String USER_BEAN = "user_bean";

    /**
     * 第三方登录存储的
     * @return
     */
    private static final String LOGIN_TYPE = "login_type";
    private static final String ACCESS_TOKEN = "access_token";
    private static final String REFRESH_TOKEN = "refresh_token";
    private static final String OPEN_ID = "open_id";
    private static final String UNION_ID = "union_id";

    /**
     * 公用的设置
     * @return
     */
    private static final String SETTINGS_PREF_NAME = "settings_pref";
    private static final String SEARCH_TYPE = "search_type"; // 0是客户端搜索， 1是服务端搜索

    public static SharedPreferences getSettingsPrefs() {
        int code = Context.MODE_MULTI_PROCESS;
        return MainApplication.getInstance().getSharedPreferences(SETTINGS_PREF_NAME, code);
    }

    public static SharedPreferences getUserPrefs() {
        int code = Context.MODE_MULTI_PROCESS;
        return MainApplication.getInstance().getSharedPreferences(USER_PREF_NAME, code);
    }

    public static void setUser(UserInfo user) {
        SharedPreferences.Editor editor = getUserPrefs().edit();
        editor.putString(USER_ID, String.valueOf(user.user_id));
        editor.putString(SRF_KEY, user.srf_key);
        editor.putString(SRF_AUTH, user.srf_auth);
        editor.putString(USERNAME, user.username);

        editor.putString(AVATAR, user.avatar);
        editor.putString(MOBILE, user.mobile);
        editor.putString(CREATED_AT, String.valueOf(user.created_at));
        editor.putString(DESCRIPTION, user.description);
        editor.putString(NICKNAME, user.nickname);

        editor.putString(CREDITS, String.valueOf(user.credits_available));
        editor.putString(USER_BEAN, new Gson().toJson(user));
        editor.apply();
    }

    /**
     * 保存登录方式 local, weixin, qq, weibo
     * @param loginType
     */
    public static void setLoginType(String loginType) {
        SharedPreferences.Editor editor = getUserPrefs().edit();
        editor.putString(LOGIN_TYPE, loginType).apply();
    }

    /**
     * 保存AccessToken
     * @param accessToken
     */
    public static void setAccessToken(String accessToken) {
        SharedPreferences.Editor editor = getUserPrefs().edit();
        editor.putString(ACCESS_TOKEN, accessToken).apply();
    }

    /**
     * refresh token
     * @param refreshToken
     */
    public static void setRefreshToken(String refreshToken) {
        SharedPreferences.Editor editor = getUserPrefs().edit();
        editor.putString(REFRESH_TOKEN, refreshToken).apply();
    }

    /**
     * open id
     * @param openId
     */
    public static void setOpenId(String openId) {
        SharedPreferences.Editor editor = getUserPrefs().edit();
        editor.putString(OPEN_ID, openId).apply();
    }

    /**
     * weixin unionid
     * @param unionId
     */
    public static void setUnionId(String unionId) {
        SharedPreferences.Editor editor = getUserPrefs().edit();
        editor.putString(UNION_ID, unionId).apply();
    }

    /**
     * 获得当前登录用户的user_id
     * @return user_id
     */
    public static String getUserId() {
        return getUserPrefs().getString(USER_ID, "");
    }

    public static String getSrfKey() {
        return getUserPrefs().getString(SRF_KEY, "");
    }

    public static String getSrfAuth() {
        return getUserPrefs().getString(SRF_AUTH, "");
    }

    public static void setUsername(String username) {
        getUserPrefs().edit().putString(USERNAME, username).apply();
    }

    public static String getUsername() {
        return getUserPrefs().getString(USERNAME, "");
    }

    public static void setAvatar(String avatar) {
        getUserPrefs().edit().putString(AVATAR, avatar).apply();
    }

    public static String getAvatar() {
        return getUserPrefs().getString(AVATAR, "");
    }

    public static String getMobile() {
        return getUserPrefs().getString(MOBILE, "");
    }

    public static String getCreatedAt() {
        return getUserPrefs().getString(CREATED_AT, "");
    }

    public static String getDescription() {
        return getUserPrefs().getString(DESCRIPTION, "");
    }

    public static void setCredits(String credits) {
        getUserPrefs().edit().putString(CREDITS, credits).apply();
    }

    public static String getCredits() {
        return getUserPrefs().getString(CREDITS, "");
    }

    /**
     *
     * @param searchType
     */
    public static void setSearchType(int searchType) {
        SharedPreferences.Editor editor = getSettingsPrefs().edit();
        editor.putInt(SEARCH_TYPE, searchType);
        editor.apply();
    }

    /**
     * 获得搜索类型
     * @return 0是客户端搜索  1是服务端进行搜索
     */
    public static int getSearchType() {
        return getSettingsPrefs().getInt(SEARCH_TYPE, 0);
    }

    /**
     * 登出要调用
     */
    public static void logout() {
        SharedPreferences.Editor editor = getUserPrefs().edit();
        editor.putString(USER_ID, "");
        editor.putString(SRF_KEY, "");
        editor.putString(SRF_AUTH, "");
        editor.putString(USERNAME, "");

        editor.putString(AVATAR, "");
        editor.putString(MOBILE, "");
        editor.putString(CREATED_AT, "");
        editor.putString(DESCRIPTION, "");
        editor.putString(NICKNAME, "");

        editor.putString(USER_BEAN, "");

        editor.putString(ACCESS_TOKEN, "");
        editor.putString(REFRESH_TOKEN, "");
        editor.putString(LOGIN_TYPE, "");
        editor.putString(OPEN_ID, "");
        editor.putString(UNION_ID, "");
        editor.apply();
    }

    /**
     * 当前是否登录了
     * @return boolean
     */
    public static boolean isLogin() {
        return !TextUtils.isEmpty(getUserPrefs().getString(USER_ID, ""));
    }
}
